import { IndexDto } from '@libs/common/dto';
import { IndexSorts } from '@libs/common/types';
import { PartialType } from '@nestjs/swagger';
import { IsDateString, IsEnum, IsNotEmpty, IsOptional } from 'class-validator';

import { AttendanceStatistic } from '../entities/attendance-statistic.entity';

type Props = keyof AttendanceStatistic;

export class AttendanceStatisticIndexDto extends IndexDto<AttendanceStatistic> {}

export interface AttendanceStatisticIndexFilterDto
  extends Omit<AttendanceStatisticIndexDto, 'sorts'> {
  sorts?: Record<Props, IndexSorts['orderType']>;
}

export class CreateAttendanceStatisticDto extends AttendanceStatistic {}

export class UpdateAttendanceStatisticDto extends PartialType(
  CreateAttendanceStatisticDto,
) {}

export class SyncAttendanceStatisticDto {
  @IsNotEmpty()
  @IsDateString()
  fromDate: string;

  /** 结束时间，结束时间减去开始时间必须在31天以内。 */
  @IsNotEmpty()
  @IsDateString()
  toDate: string;
}

export class FindAttendanceStatisticDto {
  dingId?: string;
}

export class ColumnAttendanceStatisticDto {
  dingId: string;
  /** 开始时间。 */

  @IsNotEmpty()
  @IsDateString()
  fromDate: string;

  /** 结束时间，结束时间减去开始时间必须在31天以内。 */
  @IsNotEmpty()
  @IsDateString()
  toDate: string;
}

export class StatisticAttendanceStatisticDto {
  @IsNotEmpty({ message: 'dingId 不能为空' })
  dingId: string;

  @IsDateString()
  @IsOptional()
  start?: string;

  @IsDateString()
  @IsOptional()
  end?: string;
}

export const WorkHoursStatisticTypes = [
  'day',
  'month',
  'quarter',
  'year',
] as const;

export class StatisticAttendanceWorkHoursDto extends StatisticAttendanceStatisticDto {
  @IsOptional()
  @IsEnum(WorkHoursStatisticTypes)
  type?: (typeof WorkHoursStatisticTypes)[number];
}
export class ImportAttendanceStatisticDto {
  @IsNotEmpty({ message: '日期不能为空' })
  @IsDateString({}, { message: '日期格式不正确' })
  date: string;
}
